﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
using OpenSoftStore.Orm;
using OpenSoftStore.Orm.Model;

namespace OpenSoftStore.BusinessLogic.Common
{
    public class TreeLogic : ABusinessLogic
    {
        #region Methods

        public Boolean CanUpdateMoneyTree(TreeView treeview)
        {
            var moneytree = new List<T_Tree>();
            moneytree.Add(new T_Tree { Name = "客户退套餐退款" });
            moneytree.Add(new T_Tree { Name = "客户退货退款" });
            moneytree.Add(new T_Tree { Name = "预收款退款" });
            moneytree.Add(new T_Tree { Name = "付款给供应商" });
            moneytree.Add(new T_Tree { Name = "付预付款" });
            moneytree.Add(new T_Tree { Name = "账户转出" });
            moneytree.Add(new T_Tree { Name = "支出" });
            moneytree.Add(new T_Tree { Name = "客户还款" });
            moneytree.Add(new T_Tree { Name = "退货收退款" });
            moneytree.Add(new T_Tree { Name = "客户充值" });
            moneytree.Add(new T_Tree { Name = "收回预付款" });
            moneytree.Add(new T_Tree { Name = "收预收款" });
            moneytree.Add(new T_Tree { Name = "账户转入" });
            moneytree.Add(new T_Tree { Name = "客户消费开单" });
            moneytree.Add(new T_Tree { Name = "客户办理套餐" });
            moneytree.Add(new T_Tree { Name = "收入" });
            moneytree.Add(new T_Tree { Name = "全部收支项目" });

            if (moneytree.Any(m => m.Name == treeview.SelectedNode.Text))
            {
                string message = string.Format("[{0}]是系统数据，不能修改！", treeview.SelectedNode.Text);
                MessageBox.Show(message);
                return false;
            }
            return true;
        }

        public int DeleteMoneyTree(TreeView treeview)
        {
            var moneytree = new List<T_Tree>();
            moneytree.Add(new T_Tree { Name = "客户退套餐退款" });
            moneytree.Add(new T_Tree { Name = "客户退货退款" });
            moneytree.Add(new T_Tree { Name = "预收款退款" });
            moneytree.Add(new T_Tree { Name = "付款给供应商" });
            moneytree.Add(new T_Tree { Name = "付预付款" });
            moneytree.Add(new T_Tree { Name = "账户转出" });
            moneytree.Add(new T_Tree { Name = "支出" });
            moneytree.Add(new T_Tree { Name = "客户还款" });
            moneytree.Add(new T_Tree { Name = "退货收退款" });
            moneytree.Add(new T_Tree { Name = "客户充值" });
            moneytree.Add(new T_Tree { Name = "收回预付款" });
            moneytree.Add(new T_Tree { Name = "收预收款" });
            moneytree.Add(new T_Tree { Name = "账户转入" });
            moneytree.Add(new T_Tree { Name = "客户消费开单" });
            moneytree.Add(new T_Tree { Name = "客户办理套餐" });
            moneytree.Add(new T_Tree { Name = "收入" });
            moneytree.Add(new T_Tree { Name = "全部收支项目" });

            if (moneytree.Any(m => m.Name == treeview.SelectedNode.Text))
            {
                string message = string.Format("[{0}]是系统数据，不能删除！", treeview.SelectedNode.Text);
                MessageBox.Show(message);
                return 0;
            }

            if (treeview.SelectedNode == null || treeview.SelectedNode.Level == 0) return 0;
            var tree = new T_Tree { Id = treeview.SelectedNode.Name };
            int count = this.Delete(tree);
            if (count > 0) treeview.SelectedNode.Remove();
            return count;
        }

        public int Delete(TreeView treeview)
        {
            if (treeview.SelectedNode == null || treeview.SelectedNode.Level == 0) return 0;
            var tree = new T_Tree { Id = treeview.SelectedNode.Name };
            int count = this.Delete(tree);
            if (count > 0) treeview.SelectedNode.Remove();
            treeview.Select();
            return count;
        }

        public IEnumerable<T_Tree> GetTreeByType(string rootType = "", string code = "", string treeName = "")
        {
            string where = "";
            if (!string.IsNullOrEmpty(rootType)) where = "RootType='" + rootType + "' ";
            if (!string.IsNullOrEmpty(code)) where += " and Code like '" + code + "%' ";
            if (!string.IsNullOrEmpty(treeName)) where += " and TreeName='" + treeName + "' ";
            where += " Order by Code asc";
            var data = Spa.Where<T_Tree>(where);
            return data;
        }

        public T_Tree GetTreeFirstByType(string rootType = "")
        {
            string where = "";
            if (!string.IsNullOrEmpty(rootType)) where = "RootType='" + rootType + "' ";
            where += " Order by Code asc";
            var data = Spa.FirstWhere<T_Tree>(where).ToList();
            if (!data.Any()) return null;
            var entity = data[0];
            return entity;
        }

        public IEnumerable<T_Tree> GetMemberTrees(string code = "", string treeName = "")
        {
            var data = GetTreeByType("MemberTree", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetDinnerTrees(string code = "", string treeName = "")
        {
            var data = GetTreeByType("DinnerTree", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetProductTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("ProductTree", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetMoneyTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("Money", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetProviderTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("ProviderTree", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetProviderPayTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("ProviderPayBuz", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetSetTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("SetTree", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetAdminTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("AdminTree", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetProjectTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("ProjectTree", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetStoreTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("StoreTree", code, treeName);
            return data;
        }

        public IEnumerable<T_Tree> GetRemindTree(string code = "", string treeName = "")
        {
            var data = GetTreeByType("RemindTree", code, treeName);
            return data;
        }

        public void SetTreeNode(IEnumerable<T_Tree> trees, TreeView treeView, string parentId = "", TreeNode treeNode = null)
        {
            if (string.IsNullOrEmpty(parentId)) parentId = "00000";
            List<T_Tree> childs = trees.Where(m => m.ParentId.Contains(parentId)).ToList();
            if (parentId == "00000" && !childs.Any() && trees.Any()) childs.Add(trees.ToList()[0]);
            if (!childs.Any()) return;
            foreach (var tree in childs)
            {
                var childTreeNode = new TreeNode { Text = tree.Name, Name = tree.Id, Tag = tree.Code }; //创建TNode对像 
                if (treeNode == null)
                    treeView.Nodes.Add(childTreeNode); //添加父子点 
                else
                    treeNode.Nodes.Add(childTreeNode); //添加子节点 
                SetTreeNode(trees, treeView, tree.Id, childTreeNode); //调用MyTreeView 
            }
            treeView.ExpandAll();
        }

        public void SetAdminPowerMappTreeNode(IEnumerable<T_Tree> trees, TreeView treeView, List<T_AdminPowerMapp> adminPowerMapps, string parentId = "", TreeNode treeNode = null)
        {
            if (string.IsNullOrEmpty(parentId)) parentId = "00000";
            List<T_Tree> childs = trees.Where(m => m.ParentId.Contains(parentId)).ToList();
            if (parentId == "00000" && !childs.Any() && trees.Any()) childs.Add(trees.ToList()[0]);
            if (!childs.Any()) return;
            foreach (var tree in childs)
            {
                var childTreeNode = new TreeNode { Text = tree.Name, Name = tree.Id, Tag = tree.Code }; //创建TNode对像 
                if (treeNode == null)
                    treeView.Nodes.Add(childTreeNode); //添加父子点 
                else
                    treeNode.Nodes.Add(childTreeNode); //添加子节点 
                if (adminPowerMapps.Any(m => m.TreeId == tree.Id))
                {
                    childTreeNode.Checked = true;
                }
                SetAdminPowerMappTreeNode(trees, treeView, adminPowerMapps, tree.Id, childTreeNode); //调用MyTreeView 
            }
            treeView.ExpandAll();
        }

        public Boolean SaveTreeNode(TreeView trTree, object sender, NodeLabelEditEventArgs e, string rootType)
        {
            if (string.IsNullOrEmpty(rootType) || string.IsNullOrEmpty(e.Node.Text))
            {
                return false;
            }
            var tree = new T_Tree { Id = e.Node.Name };
            if (string.IsNullOrEmpty(tree.Id))
            {
                tree.Id = Guid.NewGuid().ToString("D");
                tree.Name = e.Label == null ? e.Node.Text : e.Label;
                tree.RootType = rootType;
                tree.ParentId = e.Node.Parent.Name;
                var parentTree = Spa.Where<T_Tree>(m => m.Id == tree.ParentId).FirstOrDefault();
                var count = Spa.Where<T_Tree>(m => m.ParentId == tree.ParentId).Count();
                if (parentTree != null)
                {
                    tree.Code = parentTree.Code + count + ".";
                }
                if (Add(tree) <= 0) return false;
                trTree.SelectedNode.Text = e.Label == null ? e.Node.Text : e.Label;
                trTree.SelectedNode.Name = tree.Id;
                trTree.SelectedNode.Tag = tree.Code;
            }
            else
            {
                tree = Where<T_Tree>(m => m.Id == tree.Id).ToList()[0];
                tree.Name = e.Label;
                if (Update(tree) <= 0) return false;
            }
            return true;
        }

        #endregion
    }
}

